//供其他页面调用声明
function __g_reloadData(){
}


function initTrFilter(){
	var trnum=0;
	
	var filter={
			ip:"",
			module:"",
			level:0,
	}
	
	function getLevelStr(level){
		if(level==1){
			return "DEBUG";
		}else if(level==2){
			return "INFO";
		}else if(level==3){
			return "WARN";
		}else if(level==4){
			return "ERROR";
		}
	}
	function contain(word,filterStr){
		var index = word.indexOf(filterStr);
		if(index == -1){
			return false;
		}else{
			return true;
		}
	}
	
	function refilter(){
		filter.ip = StringUtils.trim($("[name='ip']").val());
		filter.module = StringUtils.trim($("[name='module']").val());
		filter.level = StringUtils.trim($("[name='level']").val());
		$(".table tr").each(function(){
			var $tr = $(this);
			showFilter($tr);
		});
	}
	
	function showFilter($tr){
		var msg = $tr.data("msg");
		if(!contain(msg.ip,filter.ip)){
			$tr.hide();
		}else if(!contain(msg.module,filter.module)){
			$tr.hide();
		}else if(msg.level < filter.level){
			$tr.hide();
		}else{
			$tr.show();
		}
	}
	
	function levelFilter($tr){
		var msg = $tr.data("msg");
		if(msg.level==1){
			$tr.addClass("success");
		}else if(msg.level==2){
			$tr.addClass("info");
		}else if(msg.level==3){
			$tr.addClass("warning");
		}else if(msg.level==4){
			$tr.addClass("danger");
		}
	}
	function add(msg){
		var createDate = msg.createDate;
		var contentText = msg.contentText;
		var module = msg.module;
		var ip = msg.ip;
		var levelStr =getLevelStr(msg.level);
		var trStr = StringUtils.format('<tr class=""><td>{0} {1} {2} [{3}] {4}</td></tr>',ip,module,createDate,levelStr,contentText);
		var $tr = $(trStr);
		$(".table").prepend($tr);
		$tr.data("msg",msg);
		levelFilter($tr,msg);
		showFilter($tr,msg);
	}

	return {
		"add":add,
		"refilter":refilter
	}
}




function initWebSocket(){
	var url = 'http://localhost/websocket';
//	var socket = new SockJS(url, undefined, {protocols_whitelist: ['websocket']});
	var socket = new SockJS(url);
	var stompClient = Stomp.over(socket);
	stompClient.connect({}, function(frame) {
		stompClient.subscribe('/topic/hello', function(message){
			var msg = JSON.parse(message.body);
			window.trFilter.add(msg);
			console.log(msg);
		});
	});
	socket.onclose = function() {
	     setTimeout(initWebSocket,5000);
	 };
}


function before(){
}

function init(){
	initWebSocket();
	window.trFilter = initTrFilter();
	
	$("#queryForm :input").on("keyup",function(){
		window.trFilter.refilter();
	});
	$("#queryForm select").on("change",function(){
		window.trFilter.refilter();
	});
}

function after(){
}